package com.sdehua.wear.service.impl;

import com.sdehua.wear.dao.TicketManagerDao;
import com.sdehua.wear.dao.UserDao;
import com.sdehua.wear.entity.Ticket;
import com.sdehua.wear.dao.TicketDao;
import com.sdehua.wear.entity.TicketManager;
import com.sdehua.wear.entity.User;
import com.sdehua.wear.service.TicketService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * (Ticket)表服务实现类
 *
 * @author makejava
 * @since 2022-01-03 21:11:04
 */
@Service("ticketService")
public class TicketServiceImpl implements TicketService {
    @Resource
    private TicketDao ticketDao;
    @Resource
    private TicketManagerDao ticketManagerDao;
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Ticket queryById(Integer id) {
        return this.ticketDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Ticket> queryAllByLimit(int offset, int limit) {
        return this.ticketDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param ticket 实例对象
     * @return 实例对象
     */
    @Override
    public Ticket insert(Ticket ticket) {
        this.ticketDao.insert(ticket);
        return ticket;
    }

    /**
     * 修改数据
     *
     * @param ticket 实例对象
     * @return 实例对象
     */
    @Override
    public Ticket update(Ticket ticket) {
        this.ticketDao.update(ticket);
        return this.queryById(ticket.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ticketDao.deleteById(id) > 0;
    }

    /**
     * @MethodName activeCode
     * @Author coffee
     * @Description 激活代金券
     * @Date 23:01 2022/1/3
     * @Param [code, userId]
     * @return boolean
     **/
    @Override
    public boolean activeCode(String code, String userId) {
        if (code.isEmpty() || userId.isEmpty()) return false;
        TicketManager ticketManager = ticketManagerDao.queryByCode(code);
        User user = userDao.queryById(Integer.parseInt(userId));
        if (ticketManager == null || user == null) return false;// 激活码无效 / 用户不存在
        Ticket ticket = new Ticket();
        ticket.setCode(ticketManager.getCode());
        ticket.setCount(1);
        ticket.setName(ticketManager.getName());
        ticket.setUserId(Integer.parseInt(userId));
        ticket.setPrice(ticketManager.getPrice());
        ticketManager.setCount(ticketManager.getCount()-1);
        user.setTicket(user.getTicket()+1);
        int update1 = userDao.update(user);
        int insert = ticketDao.insert(ticket);
        int update = ticketManagerDao.update(ticketManager);
        if (insert < 1 || update < 1 || update1 < 1) return false;
        return true;
    }

    @Override
    public List<Ticket> queryByUserId(String userId) {
        if (userId.isEmpty()) return null;
        return ticketDao.queryByUserId(Integer.parseInt(userId));
    }
}